package domein;

import java.io.*;
import java.util.logging.*;

public class Servlet {
    private Request req;

    public Servlet() {}

    public Servlet(Request req) {
        this.req = req;
    }

    public Response service(Request req) {
        this.req = req;
        Response resp = new Response();
        String status = "HTTP/1.1 ";
        String log = "";
        log += "----------------";
        log += "\nFile: "+req.getRequestURI();
        System.out.println(log);
        File url = new File(req.getRequestURI());
        if(url.exists()){
            resp.setCODE(200);
            resp.setREASON("OK");
            try {
                FileInputStream fis = new FileInputStream(url);
                byte[] bytes = new byte[fis.available()];
                fis.read(bytes);
                resp.setBYTES(bytes);
                
            } catch (FileNotFoundException ex) {
                System.out.println("Bestand niet gevonden");
            } catch (IOException ex) {
                System.out.println("Fout bij lezen");
            }

            //resp.setBYTES();
        } else {
            resp.setCODE(404);
            resp.setREASON("NOT FOUND");
            resp.setBYTES("404 FILE NOT FOUND".getBytes());
        }

        return resp;
    }
}
